
import { ErrorCode } from "../../../config/ErrorCode";
import { ServerConfig } from "../../../config/ServerConfig";
import { ResponseModel } from "../../../model/ResponseModel";
import { RoomManager } from "../../../server/RoomManager";
import { Logger } from "../../utils/Logger";
import { ProtoServerConfig } from "../protocol/ProtoServerConfig";
import { SocketConnection } from "./SocketConnection";
import { SocketConnectionManager } from "./SocketConnectionManager";
import { SocketMsg } from "./SocketMsg";

export class SocketClientMsgHandler {

    public static onEventSocketGata(socketConnect: SocketConnection, msg: SocketMsg) {

        if (msg.protoName == ProtoServerConfig.req_server_login) {
            this.on_req_server_login(socketConnect, msg);
        }
        else if (msg.protoName == ProtoServerConfig.req_server_set_server_status) {
            this.req_server_set_server_status(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_is_in_server) {
            this.on_req_server_is_in_server(socketConnect, msg);
        }
        else if (msg.protoName == ProtoServerConfig.req_server_is_user_online) {
            this.on_req_server_is_user_online(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_onlie_club) {
            this.on_req_server_onlie_club(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_notify_delete_club_user) {
            this.on_req_server_notify_delete_club_user(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_notify_set_club_quanxian) {
            this.req_server_notify_set_club_quanxian(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_notify_club_freeze) {
            this.req_server_notify_club_freeze(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_notify_set_club_admin) {
            this.req_server_notify_set_club_admin(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_insertRoomTemplateModel) {
            this.on_req_server_insertRoomTemplateModel(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_deleteRoomTemplateModel) {
            this.on_req_server_deleteRoomTemplateModel(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_createRoom) {
            this.on_req_server_createRoom(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_joinRoom) {
            this.on_req_server_joinRoom(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_deleteRoom) {
            this.on_req_server_deleteRoom(socketConnect, msg)
        }
        else if (msg.protoName == ProtoServerConfig.req_server_leave_room) {
            this.on_req_server_leave_room(socketConnect, msg);
        }
        else if (msg.protoName == ProtoServerConfig.req_server_notify_game_start) {
            this.on_req_server_notify_game_start(socketConnect, msg)
        }
       
    }


    private static async on_req_server_login(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let account: string = socketMsg.protoData["account"]
        new_conn.account = account
        let respModel: ResponseModel = new ResponseModel()
        respModel.code = ErrorCode.Code.Suc
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_login, respModel)
    }

    private static async req_server_set_server_status(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let status: number = socketMsg.protoData["status"]
        ServerConfig.nowServerConfig.status = status;
        let respModel: ResponseModel = new ResponseModel()
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_set_server_status, respModel)
    }

    private static async on_req_server_is_user_online(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let userid: number = socketMsg.protoData["userid"]
        Logger.log("on_req_server_is_user_online==", socketMsg)
        let isOnline: boolean = false
        if (userid && userid > 0) {
            isOnline = SocketConnectionManager.instance.isUserOnline(userid);
        }
        let result: object = {
            "isOnline": isOnline
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_is_user_online, result)
    }

    private static async on_req_server_is_in_server(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let account: string = socketMsg.protoData["account"]
        Logger.log("on_req_server_is_in_server==", socketMsg)
        let isIn: boolean = false
        if (account && account != "") {
            SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
                if (conn.isLogin()) {
                    if (conn.account == account) {
                        isIn = true
                        return
                    }
                }
            });
        }
        let result: object = {
            "isIn": isIn
        }
        Logger.log("on_req_server_is_in_server=222=", result)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_is_in_server, result)
    }


    private static async on_req_server_onlie_club(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let clubid: number = socketMsg.protoData["clubid"]
        let count: number = SocketConnectionManager.instance.getClubOnlineCount(clubid)
        let result: object = {
            "count": count
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_onlie_club, result)
    }

    private static async on_req_server_notify_delete_club_user(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let clubid: number = socketMsg.protoData["clubid"]
        let userid: number = socketMsg.protoData["userid"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_notify_delete_club_user, result)
    }

    private static async req_server_notify_club_freeze(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let clubid: number = socketMsg.protoData["clubid"]
        let userid: number = socketMsg.protoData["userid"]
        let user_state: number = socketMsg.protoData["user_state"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_notify_set_club_quanxian, result)
    }

    private static async req_server_notify_set_club_quanxian(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let clubid: number = socketMsg.protoData["clubid"]
        let userid: number = socketMsg.protoData["userid"]
        let quanxian: string = socketMsg.protoData["quanxian"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_notify_set_club_quanxian, result)
    }

    private static async req_server_notify_set_club_admin(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let clubid: number = socketMsg.protoData["clubid"]
        let userid: number = socketMsg.protoData["userid"]
        let actor: number = socketMsg.protoData["actor"]
        let quanxian: string = socketMsg.protoData["quanxian"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_notify_set_club_admin, result)
    }


    private static async on_req_server_insertRoomTemplateModel(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_insertRoomTemplateModel")
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_insertRoomTemplateModel, result)
    }


    private static async on_req_server_deleteRoomTemplateModel(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let id: number = socketMsg.protoData["id"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_deleteRoomTemplateModel")
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_deleteRoomTemplateModel, result)
    }

    private static async on_req_server_createRoom(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let room = socketMsg.protoData["room"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_createRoom", room)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_createRoom, result)
    }

    private static async on_req_server_joinRoom(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let room_id = socketMsg.protoData["room_id"]
        let playerObj = socketMsg.protoData["player"]
        let result: ResponseModel = new ResponseModel();
        Logger.log("on_req_server_joinRoom", room_id)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_joinRoom, result)
    }

    private static async on_req_server_deleteRoom(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let room_id: number = socketMsg.protoData["room_id"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_deleteRoom", room_id)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_deleteRoom, result)
    }

    private static async on_req_server_leave_room(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let room = socketMsg.protoData["room"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_leave_room", room)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_leave_room, result)
    }


    private static async on_req_server_notify_game_start(new_conn: SocketConnection, socketMsg: SocketMsg) {
        let room = socketMsg.protoData["room"]
        let result: object = {
            "code": ErrorCode.Code.Suc
        }
        Logger.log("on_req_server_notify_game_start", room)
        new_conn.createMsgToClient(socketMsg.reqId, ProtoServerConfig.req_server_notify_game_start, result)
    }




}

